package client.comm.baoding.ui

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Intent

import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import client.comm.commlib.dialog.CameraOrSelectDialog
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import client.comm.baoding.R
import client.comm.baoding.api.bean.FzInter
import client.comm.baoding.ui.vm.RechargeViewModel
import client.comm.baoding.databinding.ActivityRechargeBinding
import client.comm.baoding.databinding.LayoutSktyEeBinding
import client.comm.baoding.dialog.MsgTitleDialog

import client.comm.baoding.BaseMiddleActivity
class RechargeActivity : BaseMiddleActivity<RechargeViewModel, ActivityRechargeBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_recharge
    }

    override fun initViewModel(): RechargeViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        binding.emptyLayout.setOnClickListener {
            viewModel.callUserInfo()
        }
    }

    override fun initData() {
        viewModel.callUserInfo()
    }

    override fun observerUI() {
        viewModel.userInfo.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    viewModel.callRet()
                    binding.skWrap.removeAllViews()
                    for (i in it.data!!.pay_list) {
                        val bindingType = DataBindingUtil.inflate<LayoutSktyEeBinding>(
                            LayoutInflater.from(this),
                            R.layout.layout_skty_ee,
                            binding.skWrap,
                            false
                        ).apply {
                            i.pay_img?.let {
                                mImgLoader.load(i.pay_img).into(img)
                                img.visibility = View.VISIBLE
                            }

                            i.pay_name?.let {
                                name = i.pay_name
                                oneWrap.visibility = View.VISIBLE

                            }

                            i.pay_card?.let {
                                card = i.pay_card
                                twoWrap.visibility = View.VISIBLE

                            }

                            i.real_name?.let {
                                realName = i.real_name
                                threeWrap.visibility = View.VISIBLE
                            }
                            event = FzInter {
                                val cmb =
                                    getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
                                cmb.setPrimaryClip(ClipData.newPlainText(null, it))
                                toast("已复制")
                            }
                        }
                        binding.skWrap.addView(bindingType.root)
                    }

                    viewModel.callRet()
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess && it.data != null && it.data!!.recharge_list != null
                    && it.data!!.recharge_list.size > 0
                ) {
                    val order = it.data!!.recharge_list.get(0)
                    binding.order = order

                    if (!order.order_img.isNullOrEmpty()) {
                        mImgLoader.load(order.order_img).into(binding.img)
                        binding.okBtn.text = "重新上传"
                        binding.cancleBtn.visibility = View.GONE
                    }
                    binding.emptyLayout.visibility = View.GONE
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

        viewModel.okCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("上传成功")
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })
        viewModel.cancleRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("撤销成功")
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })
    }

    fun imgSelect() {
        CameraOrSelectDialog(this).apply {
            suc = {
                viewModel.imgOne.value = it
                mImgLoader.load(it).into(this@RechargeActivity.binding.img)
            }
        }.show()
    }

    fun ok() {
        if (viewModel.imgOne.value == null) {
            toast("请选择图片后再上传")
            return
        }
        if (binding.order == null) {
            toast("订单不得为空")
            return
        }
        viewModel.ok(this)
    }

    fun cancle() {
        MsgTitleDialog(this).apply {
            title = "是否撤销订单?"
            cancleTxt = "关闭"
            okTxt = "撤销"
            ok = {
                viewModel.cancle()
            }
        }.show()
    }

    fun goRecord() {
        startActivity(Intent(this, RechargeRecordActivity::class.java))
    }
}